<!--教师管理员报名管理页面-->
<template>
    <div>
        <div style="display: flex;align-items: center;justify-content: center">
            <el-input  style="width: 150px" placeholder="查询名称" v-model="title" clearable></el-input>
            <el-select style="width: 100px; margin: 0 10px" v-model="sign" placeholder="竞赛类型">
                <el-option
                        v-for="item in options"
                        :key="item"
                        :label="item"
                        :value="item">
                </el-option>
            </el-select>
            <el-select style="width: 150px; margin: 0 10px" v-model="teacher" placeholder="指导教师">
                <el-option
                        v-for="item in teachers"
                        :key="item"
                        :label="item"
                        :value="item">
                </el-option>
            </el-select>

            <el-button type="primary" style="margin-left: 10px" @click="load(1)">查询</el-button>
            <el-button type="info" @click="reset" plain>重置</el-button>
        </div>
<!--        <div style="margin: 10px 0">-->
<!--            <el-button type="danger" plain @click="delBatch" v-if="user.role === '管理员'">批量删除</el-button>-->
<!--        </div>-->
        <el-table :data="tableData"
                  stripe
                  :header-cell-style="{ backgroundColor: 'aliceblue', color: '#666'}"
                  style="font-size: 14px; margin-top: 20px"
                  @selection-change="handleSelectionChange"
        >
            <el-table-column width="55" align="center"></el-table-column>
            <el-table-column prop="title" label="竞赛名称" align="center"></el-table-column>
            <el-table-column prop="sign" label="竞赛类型" align="center"></el-table-column>
            <el-table-column prop="username" label="参赛者" align="center"></el-table-column>
            <el-table-column prop="major" label="学部" align="center"></el-table-column>
            <el-table-column prop="audits" v-slot="scope" label="审核资料" align="center">
                <el-button type="primary" plain @click="downloadFile(scope.row)"
                >查看</el-button>
            </el-table-column>
            <el-table-column v-slot="scope" prop="audit" label="审核状态" align="center">
                <el-tag type="primary" style="font-size: 14px"
                        v-if="scope.row.audit === '待审核'">待审核</el-tag>
                <el-tag type="success" style="font-size: 14px"
                        v-if="scope.row.audit === '已通过'">已通过
                </el-tag>
                <el-tag type="warning" style="font-size: 14px"
                        v-if="scope.row.audit === '未通过'">未通过
                </el-tag>
            </el-table-column>
            <el-table-column prop="content" label="审核内容" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="操作" align="center" width="220" v-if="user.role === '管理员'
                    || user.role === '教师'">
                <template v-slot="scope" style="font-size: 14px">
                    <el-button style="padding: 6px" size="mini" type="success" plain @click="$router.push('/enrollDetail?id=' + scope.row.id)">详情
                    </el-button>
                    <el-button style="padding: 6px" size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button style="padding: 6px" size="mini" type="danger" plain @click="del(scope.row.id)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin: 10px 0; display: flex; align-items: center; justify-content: center">
            <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-size="pageSize"
                    layout="total, prev, pager, next"
                    :total="total">
            </el-pagination>
        </div>
        <!--        弹窗-->
        <el-dialog title="报名信息" :visible.sync="fromVisible" width="50%" :close-on-click-modal="false">
            <el-form :model="form" label-width="80px" style="padding-right: 20px" :rules="rules" ref="formRef">
                <el-form-item label="竞赛标题" prop="title" >
                    <el-input disabled v-model="form.title" placeholder="竞赛标题"></el-input>
                </el-form-item>
                <el-form-item label="竞赛类型" prop="sign" >
                    <el-select disabled v-model="form.sign" placeholder="请选择竞赛类型">
                        <el-option
                                v-for="item in options"
                                :key="item"
                                :label="item"
                                :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="审核状态" prop="audit">
                    <el-select v-model="form.audit">
                        <el-option
                                v-for="item in audits"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="审核回复" prop="content">
                    <el-input type="textarea" maxlength="500" :autosize="{ minRows: 4}"
                              show-word-limit v-model="form.content" placeholder="审核回复"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="fromVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>

    </div>

</template>

<script>
    import request from "@/utils/request";

    export default {
        name: "Enroll",
        data() {
            return {
                tableData: [], //所有的数据
                pageNum: 1, //当前的页码
                pageSize: 6, //每页显示的个数
                title: '',
                sign: '',
                teacher: '',
                //竞赛类型
                options: [],
                //教师
                teachers: [],
                //审核
                audits: [{
                    value: '已通过',
                    label: '审核通过'
                }, {
                    value: '未通过',
                    label: '审核未通过'
                }],
                total: 0, //默认是0
                fromVisible: false,
                form: {},
                user: JSON.parse(localStorage.getItem("honey-user") || '{}'),//拿取个人用户信息
                rules: {
                    audit: [
                        {required: true, message: '请审核报名信息', trigger: 'blur'}
                    ],
                },
                ids: [],
                content: '',
            }
        },
        created() {
            this.load()
        },
        methods: {
            downloadFile(row){
                window.open(row.audits)
            },
            // delBatch() { //批量删除
            //     if (!this.ids.length) {
            //         this.$message.warning('请选择数据')
            //         return
            //     }
            //     this.$confirm('确认批量删除这些数据吗？', '确认删除', {type: "warning"}).then(response => {
            //         this.$request.delete('/enroll/delete/batch', {
            //             data: this.ids
            //         }).then(res => {
            //             if (res.code === '200') { //操作成功
            //                 this.$message.success('操作成功')
            //                 this.load(1) //刷新表格数据
            //             } else { //操作失败
            //                 this.$message.error(res.msg) //弹出错误信息
            //             }
            //         })
            //     }).catch(() => {
            //     })
            // },
            handleSelectionChange(rows) { //选中的数据 rows:当前选中的所有行数据
                this.ids = rows.map(v => v.id)
            },
            del(id) { //单个删除
                this.$confirm('确认删除？', '确认删除', {type: "warning"}).then(response => {
                    this.$request.delete('/enroll/delete/' + id).then(res => {
                        if (res.code === '200') { //操作成功
                            this.$message.success('操作成功')
                            this.load(1) //刷新表格数据
                        } else { //操作失败
                            this.$message.error(res.msg) //弹出错误信息
                        }
                    })
                }).catch(() => {
                })
            },
            handleEdit(row) { //编辑
                this.form = JSON.parse(JSON.stringify(row)) //给form对象赋值 注意要深拷贝数据
                this.form.audit = ''
                this.fromVisible = true //打开弹窗
            },
            handleAdd() {
                this.form = {} //新增数据的时候清空数据
                this.fromVisible = true //打开新增弹窗
            },
            save() { //保存
                this.$refs.formRef.validate((valid) => {
                    if (valid) { //验证通过
                        this.$request({
                            url: this.form.id ? '/enroll/update' : '/enroll/add',
                            method: this.form.id ? 'PUT' : 'POST', //判断有无id,有为编辑,无为新增
                            data: this.form
                        }).then(res => {
                            if (res.code === '200') { //成功保存
                                this.$message.success('保存成功')
                                this.load(1)
                                this.fromVisible = false
                            } else { //保存失败
                                this.$message.error(res.msg) //弹出错误信息
                            }
                        })

                    }
                })
            },
            reset() {
                this.title = ''
                this.sign = ''
                this.teacher = ''
                this.load()
            },
            load(pageNum) { //分页查询
                if (pageNum) {
                    this.pageNum = pageNum
                }
                this.$request.get('/enroll/selectByPage', {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        title: this.title,
                        sign: this.sign,
                        teacher: this.teacher,
                    }
                }).then(res => {
                    this.tableData = res.data.records
                    console.log(this.tableData)
                    this.total = res.data.total
                })
                //接收竞赛类型值
                this.$request.get('/sign/selectAll').then(res => {
                    this.options = res.data
                })
                //接收教师值
                this.$request.get('/user/selectTeacher').then(res => {
                    this.teachers = res.data
                    console.log(this.teachers)
                })
            },
            handleCurrentChange(pageNum) {
                this.load(pageNum)
            },
        }
    }

</script>

<style scoped>
    .el-tooltip__popper {
        max-width: 400px;
    }

</style>